package leetcode.L529;

class Solution {
    int[] dirX={-1,0,1,-1,1,-1,0,1};
    int[] dirY={-1,-1,-1,0,0,1,1,1};
    public char[][] updateBoard(char[][] board, int[] click) {
        //深度优先遍历
        int x = click[0];
        int y = click[1];
        if (board[x][y] == 'M') {
            board[x][y] = 'X';
        } else {
            dfs(board,x,y);
        }
        return board;
    }

    public void dfs(char[][] board, int x, int y) {
        int cns = 0;
        for (int i = 0; i < 8; i++) {
            int tx = x + dirX[i];
            int ty = y + dirY[i];
            if (tx < 0 || tx >= board.length || ty < 0 || ty >= board[0].length) {
                continue;
            }
            if (board[tx][ty] == 'M') {
                cns++;
            }
        }
        if (cns > 0) {
            //规则3
            board[x][y] = (char) (cns + '0');
        } else {
            //规则2
            board[x][y] = 'B';
            for (int i = 0; i < 8; ++i) {
                int tx = x + dirX[i];
                int ty = y + dirY[i];
                // 这里不需要在存在 B 的时候继续扩展，因为 B 之前被点击的时候已经被扩展过了
                if (tx < 0 || tx >= board.length || ty < 0 || ty >= board[0].length || board[tx][ty] != 'E') {
                    continue;
                }
                dfs(board, tx, ty);
            }
        }
    }
}